package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.dto.RoleDto;
import com.itheima.pojo.Role;
import com.itheima.service.RoleUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.RoleUserMapper;
import com.itheima.pojo.RoleUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
@DubboService
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements RoleUserService {


    @Override
    @Transactional
    public Boolean saveByUserIds(RoleDto roleDto) {
        Long roleId = roleDto.getRole().getId();
        Long[] ids = roleDto.getIds();
        ArrayList<RoleUser> roleUsers = new ArrayList<>();
        for (Long userId : ids) {
            RoleUser roleUser = new RoleUser();
            roleUser.setRoleId(roleId);
            roleUser.setUserId(userId);
            roleUsers.add(roleUser);
        }
        QueryWrapper<RoleUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        this.remove(queryWrapper);
        this.saveOrUpdateBatch(roleUsers);
        return true;
    }

    @Override
    public Boolean deleteRoleUserByRoleId(Long id) {
        QueryWrapper<RoleUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        return this.remove(queryWrapper);
    }

    @Override
    public Long[] findRoleIdByUserId(Long userId) {
        return baseMapper.findRoleIdByUserId(userId);
    }


}